package com.zhitong.money.services;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhitong.money.entity.Message;
import com.zhitong.money.exception.AddException;
import com.zhitong.money.exception.DeleteException;
import com.zhitong.money.exception.UpdateException;
import com.zhitong.money.mapper.MessageMapper;
import com.zhitong.money.mapper.MsgEmpMapper;
import com.zhitong.money.services.api.IMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @Author: Andy Zhang
 * @Description:
 * @Date: 16:43 2021/2/25
 * @Company: wisecard
 */
@Service
public class MessageService implements IMessageService {

    @Autowired
    private MessageMapper messageMapper;

    @Autowired
    private MsgEmpMapper msgEmpMapper;


    @Override
    public List<Message> findAllMsg() throws Exception {
        List<Message> allMsg = messageMapper.findAllMsg();
        return allMsg;
    }

    @Override
    public List<Message> findMsgByMap(Map<String, Object> map) throws Exception {
        List<Message> msgByMap = messageMapper.findMsgByMap(map);
        return msgByMap;
    }

    @Override
    public List<Message> findSelfMsg(String tid) throws Exception {
        Map<String,Object> map=new HashMap<>();
        map.put("tid",tid);
        map.put("status","01");
        List<Message> msgByMap = messageMapper.findMsgByMap(map);
        return msgByMap;
    }

    @Override
    public PageInfo<Message> findMsgByPage(Map<String, Object> map) throws Exception {
        int pageNum = map.containsKey("pageNum")?(int)map.get("pageNum"):1;
        int pageSize = map.containsKey("pageSize")?(int)map.get("pageSize"):5;
        PageHelper.startPage(pageNum,pageSize);
        List<Message> msgByMap = messageMapper.findMsgByMap(map);
        PageInfo<Message> pageInfo=new PageInfo<>(msgByMap);
        return pageInfo;
    }

    @Override
    public Message findMsgById(String id) throws Exception {
        Message msgById = messageMapper.findMsgById(id);
        if (msgById==null){
            throw new NullPointerException("findMsgById:查询为空");
        }
        return msgById;
    }

    @Transactional
    @Override
    public void addMsg(Map<String,Object> map) throws Exception {
        try {
            String id = UUID.randomUUID().toString().replace("-","");
            List<String> receivers =(List<String>) map.get("receivers");
            for (String receiver : receivers) {
                msgEmpMapper.addRelated(id,receiver);
            }
            map.put("id", id);
            messageMapper.addMsg(map);
        } catch (Exception e) {
            throw new AddException("addMsg:添加消息失败"+e.getMessage());
        }
    }

    @Override
    public void updateMsg(Message message) throws Exception {
        try {
            messageMapper.updateMsg(message);
        } catch (Exception e) {
            throw new UpdateException("updateMsg:更新失败!"+e.getMessage());
        }
    }

    @Override
    public void delBySelf(String id) throws Exception {
        try {
            messageMapper.updateStatus(id,"00");
        } catch (Exception e) {
            throw new UpdateException("updateStatus:更新失败!"+e.getMessage());
        }
    }

    @Transactional
    @Override
    public void delMsgById(String id) throws Exception {
        try {
            //先删除与此条信息的关联
            msgEmpMapper.delByMid(id);

            //再删除消息
            messageMapper.delMsgById(id);
        } catch (Exception e) {
            throw new DeleteException("delMsgById:删除失败"+e.getMessage());
        }
    }

    @Transactional
    @Override
    public void delMsgByIds(List<String> ids) throws Exception {
        try {

            msgEmpMapper.delByMids(ids);

            messageMapper.delMsgByIds(ids);
        } catch (Exception e) {
            throw new DeleteException("delMsgByIds:删除失败"+e.getMessage());
        }
    }


}
